package content

import (
	"context"
	"database/sql"
	"errors"
	"github.com/gogf/gf/v2/frame/g"
	"video-api/internal/dao"
	"video-api/internal/model/entity"
	"video-api/internal/model/input"
	"video-api/internal/model/out"
	"video-api/internal/service"
	"video-api/utility"
	"video-api/utility/cdn_url"
)

func init() {
	service.RegisterContentService(&contentService{})
}

type contentService struct{}

// GetBannerList 获取轮播图列表
func (s *contentService) GetBannerList(ctx context.Context, param *input.BannerList) ([]*out.Banner, error) {
	appInfo := new(entity.App)
	err := dao.App.Ctx(ctx).Where("version_no = ? ", param.Version).Scan(appInfo)
	if err != nil && errors.Is(err, sql.ErrNoRows) {
		return nil, err
	}
	if appInfo.Id == 0 {
		return nil, errors.New("未找到app版本信息")
	}
	appBanners := make([]entity.AppBanner, 0)
	appBannerModel := dao.AppBanner.Ctx(ctx)
	err = appBannerModel.Where("app_id = ?", appInfo.Id).Scan(&appBanners)
	if err != nil {
		g.Log().Error(ctx, "contentService-GetBannerList-GetByAppId err：", err, g.Map{"id": appInfo.Id})
		return nil, err
	}
	bannerIds := make([]int, len(appBanners))
	for i, appBanner := range appBanners {
		bannerIds[i] = appBanner.BannerId
	}
	//bannerIds = utility.DeDuplicateSlice(bannerIds)
	banners := make([]entity.Banner, 0)
	bannerModel := dao.Banner.Ctx(ctx)
	err = bannerModel.WhereIn("id", bannerIds).Where("enable = 1").Scan(&banners)
	if err != nil {
		return nil, err
	}
	res := make([]*out.Banner, len(banners))
	for i, banner := range banners {
		res[i] = new(out.Banner)
		res[i].Id = banner.Id
		res[i].Name = banner.Name
		res[i].Enable = banner.Enable
		prefix := g.Cfg().MustGet(ctx, "cdnurl.url").String()
		cdnUrl, err := cdn_url.CdnUrl(prefix, banner.CoverUrl)
		if err != nil {
			g.Log().Error(ctx, "contentService-GetBannerList-CdnUrl err：", err, g.Map{"url": banner.CoverUrl})
			return nil, err
		}
		res[i].CoverUrl = cdnUrl.Url
		res[i].JumpId = banner.JumpId
		res[i].JumpType = banner.JumpType
		res[i].LanguageId = banner.LanguageId
		res[i].Sort = banner.Sort
	}
	return res, nil
}

// GetTagsList 标签列表
func (s *contentService) GetTagsList(ctx context.Context, param *input.TagList) ([]*out.Tags, error) {
	tags := make([]*out.Tags, 0)
	tagModel := dao.Tags.Ctx(ctx)
	if len(param.Ids) > 0 {
		tagModel.WhereIn("id", param.Ids)
	}
	err := tagModel.Scan(&tags)
	if err != nil {
		g.Log().Error(ctx, "contentService-GetTagsList-GetAll err：", err)
		return nil, err
	}
	return tags, nil
}

// GetCategoriesList 短剧分类列表
func (s *contentService) GetCategoriesList(ctx context.Context, param *input.CategoriesList) ([]*out.Categories, error) {
	tags := make([]*out.Categories, 0)
	tagModel := dao.Categories.Ctx(ctx)
	err := tagModel.Scan(&tags)
	if err != nil {
		g.Log().Error(ctx, "contentService-GetTagsList-GetAll err：", err)
		return nil, err
	}
	return tags, nil
}

// GetShortDramaTagsList 短剧标签对应列表
func (s *contentService) GetShortDramaTagsList(ctx context.Context, param *input.ShortDramaTagList) ([]*out.ShortDramaTags, error) {
	tags := make([]*out.ShortDramaTags, 0)
	tagModel := dao.ShortDramaTags.Ctx(ctx)
	err := tagModel.WhereIn("short_drama_id", param.ShortDramaIds).Scan(&tags)
	if err != nil {
		g.Log().Error(ctx, "contentService-GetTagsList-GetAll err：", err)
		return nil, err
	}
	tagIds := make([]int64, len(tags))
	for i := range tags {
		tagIds = append(tagIds, tags[i].TagId)
	}
	tagIds = utility.DeDuplicateSlice(tagIds)
	tagInfos := make([]*out.Tags, 0)
	if len(tagIds) > 0 {
		tagInfos, err = s.GetTagsList(ctx, &input.TagList{Ids: tagIds})
		if err != nil {
			g.Log().Error(ctx, "contentService-GetShortDramaTagsList-GetTagsList err：", err)
			return nil, err
		}
	}
	for j := range tags {
		for _, tagInfo := range tagInfos {
			if tagInfo.Id == tags[j].TagId {
				tags[j].TagName = tagInfo.Title
			}
		}
	}
	return tags, nil
}
